package com.inmaytide.website.utils.log;

import com.inmaytide.website.service.system.SysLogService;
import com.inmaytide.website.utils.log.annotation.LogInformation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 日志切面
 *
 * @author luomiao
 * @since May 24, 2016
 */
@Aspect
@Component
public class LogAspect {

    @Resource
    private SysLogService logService;

    private LogInformation findLogInformationAnnotation(JoinPoint point) {
        MethodSignature signature = (MethodSignature) point.getSignature();
        return signature.getMethod().getAnnotation(LogInformation.class);
    }

    @Before(value = "@annotation(com.inmaytide.website.utils.log.annotation.LogInformation)")
    public void beforeAdvice(JoinPoint point) {

    }

    @AfterReturning(value = "@annotation(com.inmaytide.website.utils.log.annotation.LogInformation)")
    public void afterReturnAdvice(JoinPoint point) {
        logService.insert(findLogInformationAnnotation(point), point.getSignature().getName());
    }

    @AfterThrowing(value = "@annotation(com.inmaytide.website.utils.log.annotation.LogInformation)", throwing = "e")
    public void afterThrowAdvice(JoinPoint point, Throwable e) {
        logService.insert(findLogInformationAnnotation(point), e, point.getSignature().getName());
    }
}	
